#include <iostream>
#include <queue>
using namespace std;

struct node{
	int x,y,t;
	bool operator < (const node & a) const{
		return t>a.t;
	}
};



int find(int *f,int n){
	if(f[n] == n)
		return n;
	f[n] = find(f,f[n]);
	return f[n];
}

bool cac(int *f,int n){
	int l=find(f,f[0]);
	for(int i=1;i<n;i++)
		if(find(f,f[i]) != l){
//			cout<<i<<endl;
			return false;
		}
	return true;
}

void log(int *f, int n){
	cout<<endl<<"f:";
	for(int j=0;j<n;j++)
		cout<<" "<<f[j];
	cout<<endl;
}

int main(){
	int n,m;
	cin>>n>>m;
	priority_queue<node> q;
	int f[n];
	for(int i=0;i<n;i++)
		f[i] = i;
	
	
	for(int i=0;i<m;i++){
		int a,b,t;
		cin>>a>>b>>t;
		a--;b--;
		node l;
		l.t = t;
		l.x = a;
		l.y = b;
		q.push(l);
	}
//	log(f, n);
	for(int i=0;i<m;i++){
		int a,b;
		node l;
		l = q.top();
		q.pop();
		a = l.x;
		b = l.y;
		f[find(f,a)] = find(f,b);
//		log(f, n);
		if(cac(f,n)){
			cout<<l.t;
			return 0;
		}
	}
	cout<<-1;
	return 0;
}
